<template>
    <div class="white-box">
        <div class="table-mb">
            <el-button type="primary" @click="clickUpdate(1)">添加</el-button>
            <!-- <el-button type="default" plain @click="allDel">批量删除</el-button> -->
        </div>

         <!-- @selection-change="handleAllSelected" -->
        <el-table
            class="table-mb"
            v-loading="loading"
            :data="arr_data"
            :header-cell-style="{ background: '#F5F7FA' }"
            element-loading-background="rgba(255, 255, 255, 0.6)"
        >
            <!-- <el-table-column type="selection" width="55" /> -->
            <el-table-column prop="extra_id" label="ID" width="80"></el-table-column>
            <el-table-column prop="title" label="需求标题" width="190"></el-table-column>
            <el-table-column prop="content" label="说明"></el-table-column>
            <el-table-column label="排序" width="150">
                <template #default="{ row }">
                    <el-input
                        v-model.trim="row.order_by"
                        clearable
                        placeholder="请输入排序数字"
                        oninput="value=value.replace(/[^\d]/g,'')"
                        @change="() => inputSort(row)"
                        style="width: 100px"
                    />
                </template>
            </el-table-column>
            <el-table-column prop="created_at_label" label="添加时间" width="160"></el-table-column>
            <el-table-column label="操作" width="160" class-name="operation-nav" fixed="right">
                <template #default="{ row }">
                    <el-button class="theme-btn" link type="primary" @click="clickUpdate(2, row)">编辑 </el-button>
                    <el-popconfirm title="是否确定删除？" confirm-button-text="确定" cancel-button-text="取消" @confirm="handleDelete(row)">
                        <template #reference>
                            <el-button class="theme-btn" link type="primary">删除</el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
            background
            v-model:current-page="page"
            v-model:page-size="pageSize"
            :page-sizes="pageSizeNum"
            :total="totalNums"
            layout="total, sizes, prev, pager, next, jumper"
        >
        </el-pagination>

        <!-- 添加/编辑 -->
        <el-dialog
            v-model="dialogZd"
            :title="classType == 1 ? '添加特殊需求' : '编辑特殊需求'"
            width="620px"
            :append-to-body="true"
            :destroy-on-close="true"
            :close-on-click-modal="false"
            @close="closeAddZd"
        >
            <el-form ref="ruleZdRef" :model="ruleZdForm" :rules="ruleCheckZd" label-width="150px">
                <el-form-item label="特殊需求标题" prop="title">
                    <el-input v-model.trim="ruleZdForm.title" clearable placeholder="请输入特殊需求标题" />
                </el-form-item>
                <el-form-item label="特殊需求说明" prop="remark" required>
                    <el-input
                        v-model.trim="ruleZdForm.remark"
                        type="textarea"
                        :rows="7"
                        placeholder="请输入特殊需求说明"
                    />
                </el-form-item>
                <el-form-item label="排序" prop="sort" required>
                    <el-input v-model.number="ruleZdForm.sort" clearable oninput="value=value.replace(/[^\d]/g,'')" placeholder="请输入排序数字" />
                </el-form-item>
                <el-form-item>
                    <div class="flexs" style="margin-top: 20px">
                        <el-button type="primary" :loading="form_loading" @click="submitZdForm(ruleZdRef)">
                            {{ classType == 1 ? "确认添加" : "确认修改" }}
                        </el-button>
                        <el-button @click="resetZdForm(ruleZdRef)">重置</el-button>
                    </div>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>

<script setup>
import { getCurrentInstance, ref, onMounted, watch, reactive,onBeforeUnmount } from "vue"
import OneLoad from "@mixins/one-load"

const { proxy } = getCurrentInstance()

onMounted(() => {
    getList()
})

onBeforeUnmount(() => {
    
})

/**
 * minxin列表加载
 */
const { page, pageSizeNum, pageSize, loading, arr_data, totalNums, pubParams, getdata } = OneLoad()
// 切换每页显示条数
watch(pageSize, (val) => {
    page.value = 1
    pubParams(getList)
})
// 跳转第几页
watch(page, (val) => {
    pubParams(getList)
})

function getList() {
    let params = {
        page: page.value,
        limit: pageSize.value,
    }
    getdata("extra/index", params)
}

/**
 * 添加/编辑
 * @property {Boolean} dialogZd 添加/编辑弹窗状态
 * @property {Boolean} form_loading 提交按钮状态
 * @property {Number} classType 弹窗类型，1是添加，2是编辑
 * @property {Array} ruleZdRef 表单规则
 * @property {Object} ruleZdForm  表单字段
 * @property {Object} ruleCheckZd  表单提示
 * @property {Function} submitZdForm 提交表单
 * @property {Function} addZd 添加分类
 * @property {Function} closeAddZd 关闭弹窗
 */
const dialogZd = ref(false)
const form_loading = ref(false)
const classType = ref(1)
const ruleZdRef = ref(null)
const ruleZdForm = reactive({
    id: "",
    title: "",
    remark: "",
    sort: "100",
})
const ruleCheckZd = reactive({
    title: [{ required: true, message: "请输入特殊需求标题", trigger: "blur" }],
    remark: [{ required: true, message: "请输入特殊需求说明", trigger: "blur" }],
    sort: [{ required: true, message: "请输入排序", trigger: "blur" }],
})
const submitZdForm = (formEl) => {
    if (!formEl) return
    formEl.validate((valid, fields) => {
        if (valid) {
            form_loading.value = true
            let data = {
                title: ruleZdForm.title,
                content: ruleZdForm.remark,
                order_by: ruleZdForm.sort,
            }
            if (classType.value == 2) {
                data.extra_id = ruleZdForm.id
            }
            let url = classType.value == 1 ? "extra/add" : "extra/update"
            proxy.$axios
                .post(url, data)
                .then((res) => {
                    form_loading.value = false
                    if (res.data.code == 0) {
                        dialogZd.value = false
                        proxy.$message({ message: `${classType.value == 1 ? '添加成功！' : '编辑成功！'}`, type: "success" })
                        pubParams(getList)
                    } else {
                        proxy.$message({message:res.data.msg,type:"error"})
                    }
                })
                .catch((err) => {
                    form_loading.value = false
                })
        }
    })
}
const resetZdForm = (formEl) => {
    if (!formEl) return
    formEl.resetFields()
}

// 添加/编辑
const clickUpdate = (type, row) => {
    classType.value = type
    dialogZd.value = true
    if (type == 2) {
        setTimeout(() => {
            ruleZdForm.id = row.extra_id
            ruleZdForm.title = row.title
            ruleZdForm.remark = row.content
            ruleZdForm.sort = row.order_by
        }, 50)
    }
}
const closeAddZd = () => {
    dialogZd.value = false
    resetZdForm(ruleZdRef.value)
}

// 删除单个
const handleDelete = (row) => {
    postDel(row.extra_id)
}
// 删除多个
const ids = ref([])
const handleAllSelected = (val) => {
    let arrs = []
    for (let i = 0; i < val.length; i++) {
        arrs.push(val[i].extra_id)
    }
    ids.value = arrs
}
const allDel = () => {
    if (ids.value.length == 0) {
        proxy.$message({ message: "请选择需要删除的数据", type: "warning" })
        return
    }
    postDel(ids.value)
}
// 删除接口
function postDel(params) {
    proxy.$axios
        .post("extra/delete", {
            extra_id: params,
        })
        .then((res) => {
            if (res.data.code == 0) {
                pubParams(getList)
            } else {
                proxy.$message({message:res.data.msg,type:"error"})
            }
        })
        .catch((err) => {})
}

// 排序
const inputSort = (row) => {
    if (!row.order_by) {
        proxy.$message({ message: "请输入排序数字", type: "warning" })
        return
    }
    proxy.$axios
        .post("extra/sort", {
            extra_id: row.extra_id,
            order_by: row.order_by,
        })
        .then((res) => {
            if (res.data.code == 0) {
                pubParams(getList)
            } else {
                proxy.$message({message:res.data.msg,type:"error"})
            }
        })
        .catch((err) => {})
}
</script>
<script>
export default {
    name: "SpecialNeeds",
}
</script>

<style lang="less" scoped></style>
